package com.helloae.jmx;

import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;

public class AgentServer {
	private static Registry registry = null; // 注册服务

	public static void main(String[] args) throws InstanceAlreadyExistsException,
			MBeanRegistrationException, NotCompliantMBeanException,
			MalformedObjectNameException, NullPointerException,
			IOException {
		try {
			registry = LocateRegistry.createRegistry(3721); // 注册端口
		} catch (RemoteException e) {
			return;
		}
		MBeanServer server = MBeanServerFactory.createMBeanServer();

		server.registerMBean(new Hello(), new ObjectName("MBeans:type=Hello"));

		JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://"
				+ "192.168.3.106" + ":" + "3721" + "/server");
		JMXConnectorServer cs = JMXConnectorServerFactory
				.newJMXConnectorServer(url, null, server);
		cs.start();
		System.out.println("jmx has started!");
	}
}
